package com.wanpinghui.supplier.object;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.common.BaseActivity;
import com.wanpinghui.supplier.object.adapter.FeedBackReasonAdapter;
import com.wanpinghui.supplier.widgets.TitlePanelLayout;
import com.wanpinghui.wphlibrary.WPHSdk;
import com.wanpinghui.wphlibrary.model.object.ObjectService;
import com.wanpinghui.wphlibrary.model.object.bean.FeedBackReasonBean;
import com.wanpinghui.wphlibrary.widgets.ThrottleButton;
import com.yunshl.yunshllibrary.utils.TextUtil;
import com.yunshl.yunshllibrary.view.YunShlEditText;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.List;

import retrofit.callback.YRequestCallback;

/***
 * 功能描述:
 * 作者:qiujialiu
 * 时间:2017/12/2
 ***/
@ContentView(R.layout.layout_activity_feedback_new)
public class FeedBackNewActivity extends BaseActivity {
    @ViewInject(R.id.tpl_title)
    private TitlePanelLayout mLayoutTitle;
    @ViewInject(R.id.tv_reason1)
    private TextView textViewReason1;
    @ViewInject(R.id.tv_reason2)
    private TextView textViewReason2;
    @ViewInject(R.id.tv_reason3)
    private TextView textViewReason3;
    @ViewInject(R.id.btn_submit)
    private ThrottleButton mButtonSubmit;
    @ViewInject(R.id.recv_reason)
    private RecyclerView mRecyclerView;
    @ViewInject(R.id.ll_input)
    private LinearLayout mLayoutInput;
    @ViewInject(R.id.tv_left_count)
    private TextView mTextViewLeftCount;
    @ViewInject(R.id.edt_input)
    private YunShlEditText mEditTextInput;
    private int tab = 1;
    private long demandId;
    private FeedBackReasonAdapter mAdapter;
    private List<FeedBackReasonBean> datas;

    @Override
    public String getName() {
        return null;
    }

    @Override
    public boolean isBar() {
        return false;
    }

    @Override
    public void initViews() {
        if (getIntent() != null) {
            demandId = getIntent().getLongExtra("id", 0);
        }
        mAdapter = new FeedBackReasonAdapter(this);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    public void bindEvents() {
        textViewReason1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setCurrentTab(1);
            }
        });

        textViewReason2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setCurrentTab(2);
            }
        });

        textViewReason3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setCurrentTab(3);
            }
        });

        mLayoutTitle.setOnClickBacktrack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mButtonSubmit.setOnThrottleClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (tab == 1) {
                    feedback(mAdapter.getSelectData().key, null);
                } else if (tab == 2) {
                    feedback(mAdapter.getSelectData().key, null);
                } else if (tab == 3) {
                    if (TextUtil.isNotEmpty(mEditTextInput.getTextString())) {
                        feedback(99, mEditTextInput.getTextString());
                    } else {
                        showWarningText("请输入问题描述");
                    }
                }
            }
        });
        mEditTextInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                mTextViewLeftCount.setText(s.length() + "/100");
            }
        });

    }

    public void feedback(int key, String content) {
        WPHSdk.getService(ObjectService.class)
                .objectFeedback(demandId, key, content, new YRequestCallback<Object>() {
                    @Override
                    public void onSuccess(Object var1) {
                        setResult(RESULT_OK);
                        finish();
                    }

                    @Override
                    public void onFailed(int var1, String message) {
                        showWarningText(message);
                    }

                    @Override
                    public void onException(Throwable var1) {
                        var1.printStackTrace();
                    }
                });
    }

    private void setCurrentTab(int i) {
        if (i == 1) {
            tab = 1;
            textViewReason1.setSelected(true);
            textViewReason2.setSelected(false);
            textViewReason3.setSelected(false);
            if (datas != null && datas.size() > 0) {
                mAdapter.setDatas(datas.get(0).getDatas());
            }
            mRecyclerView.setVisibility(View.VISIBLE);
            mLayoutInput.setVisibility(View.GONE);
        } else if (i == 2) {
            tab = 2;
            textViewReason1.setSelected(false);
            textViewReason2.setSelected(true);
            textViewReason3.setSelected(false);
            if (datas != null && datas.size() > 1) {
                mAdapter.setDatas(datas.get(1).getDatas());
            }
            mRecyclerView.setVisibility(View.VISIBLE);
            mLayoutInput.setVisibility(View.GONE);
        } else if (i == 3) {
            mRecyclerView.setVisibility(View.GONE);
            mLayoutInput.setVisibility(View.VISIBLE);
            tab = 3;
            textViewReason1.setSelected(false);
            textViewReason2.setSelected(false);
            textViewReason3.setSelected(true);
        }
    }

    @Override
    public void initData() {
        setCurrentTab(1);
        WPHSdk.getService(ObjectService.class)
                .getFeedBackType(new YRequestCallback<List<FeedBackReasonBean>>() {
                    @Override
                    public void onSuccess(List<FeedBackReasonBean> var1) {
                        datas = var1;
                        if (tab == 1) {
                            mAdapter.setDatas(datas.get(0).getDatas());
                        } else if (tab == 1) {
                            mAdapter.setDatas(datas.get(1).getDatas());
                        }
                    }

                    @Override
                    public void onFailed(int var1, String message) {
                        showWarningText(message);
                    }

                    @Override
                    public void onException(Throwable var1) {
                        var1.printStackTrace();
                    }
                });
    }
}
